package cn.com.goldhs.architecture.ui;

import java.util.ResourceBundle;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import cn.com.goldhs.architecture.inter.Base;

public class DataEntryDialog {

	// resource
	private static ResourceBundle res = ResourceBundle.getBundle("architecture");
	
	// shell
	private Shell shell = null;
	
	// control variable
	private Button okButton;
	private Button cancelButton;
	
	// other variable
	private int userSelect = SWT.CANCEL;
	private Base parent = null;
	private String name = null;
	
	// constants
	public DataEntryDialog(Shell shell, Base parent) {
		this.shell = new Shell(shell, SWT.SHELL_TRIM | SWT.PRIMARY_MODAL);
		this.shell.setLayout(new GridLayout());
		this.shell.addShellListener(new ShellAdapter() {
			@Override
			public void shellClosed(ShellEvent e) {
				cancel();
			}
		});
		this.parent = parent;
	}
	
	public int open(){
		createTextWidgets();
		createControlButtons();
		
		shell.pack();
		shell.open();
		
		Display display = shell.getDisplay();
		
		while(!shell.isDisposed()){
			if(!display.readAndDispatch()){
				display.sleep();
			}
		}
		
		return userSelect;
	}

	private void createTextWidgets() {
		GridData data = null;
		GridLayout layout = null;
		
		Composite composite = new Composite(shell, SWT.NONE);
		data = new GridData(GridData.FILL_HORIZONTAL);
		composite.setLayoutData(data);
		layout = new GridLayout(2, false);
		composite.setLayout(layout);
		
		Label label = new Label(composite, SWT.RIGHT);
		label.setText(res.getString("Architecture.Up"));
		
		data = new GridData(150, SWT.DEFAULT);
		Text text = new Text(composite, SWT.BORDER);
		text.setLayoutData(data);
		text.setEnabled(false);
		if(parent != null)
			text.setText(parent.getName());
		
		label = new Label(composite, SWT.RIGHT);
		label.setText(res.getString("Architecture.Down"));
		
		text = new Text(composite, SWT.BORDER);
		text.setLayoutData(data);
		if(name != null) {
			text.setText(name);
		}
		text.addModifyListener(new ModifyListener() {
			
			@Override
			public void modifyText(ModifyEvent e) {
				Text t = (Text) e.widget;
				name = t.getText().trim();
				okButton.setEnabled(!name.trim().equals(""));
			}
		});
		
	}
	
	private void createControlButtons() {
		Composite composite = new Composite(shell, SWT.NONE);
		composite.setLayout(new GridLayout(2, true));
		composite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
		
		okButton = new Button(composite, SWT.PUSH);
		okButton.setLayoutData(new GridData(75, SWT.DEFAULT));
		okButton.setEnabled(false);
		okButton.setText(res.getString("Window.Dialog.OK"));
		okButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				userSelect = SWT.OK;
				shell.dispose();
			}
		});
		
		cancelButton = new Button(composite, SWT.PUSH);
		cancelButton.setLayoutData(new GridData(75, SWT.DEFAULT));
		cancelButton.setText(res.getString("Window.Dialog.Cancel"));
		cancelButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				cancel();
				shell.close();
			}
		});
		
		shell.setDefaultButton(okButton);
	}
	
	protected void cancel() {
		name = null;
		userSelect = SWT.CANCEL;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public String getName() {
		return name;
	}

}